﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface ICmdReader {
    void SendCmd(CmdBase cmd);
}

public interface ICmdSender {
    void SetCmdReader(ICmdReader reader);
}

public class CmdBase {
    public static int TYPE_MOVE = 0;
    public static int TYPE_FIRE = 1;
    public static int TYPE_JUMP = 2;
    public int Type;
}

public class CmdMove :CmdBase {

    public static int ORI_RIGHT = 1;
    public static int ORI_LEFT = -1;
    public CmdMove(int ori) {
        Type = CmdBase.TYPE_MOVE;
        Oir = ori;
    }
    public int Oir;
}

public class CmdFire : CmdBase {
    public CmdFire() {
        Type = CmdBase.TYPE_FIRE;
    }
}
public class CmdJump : CmdBase
{
    public CmdJump() {
        Type = CmdBase.TYPE_JUMP;
    }
}

public delegate void GetCmdReader(ICmdReader cmdr);
public class PlayerCtrl : MonoBehaviour, ICmdSender
{

    private ICmdReader cmdReader_ = null;

    public void SetCmdReader(ICmdReader reader)
    {
        this.cmdReader_ = reader;
    }

    private void getCmdReader(GetCmdReader cb) {
        if (this.cmdReader_ != null) {
            if (cb!= null) {
                cb(this.cmdReader_);
            }
        }
    }

    public bool isAKeyDown_ = false;
    public bool isDKeyDown_ = false;
    // Update is called once per frame
    void Update()
    {
        var MIN_ABS = 0.1;
        if (Input.GetKeyDown(KeyCode.A)) {
            isAKeyDown_ = true;
        }
        if (Input.GetKeyUp(KeyCode.A)) {
            isAKeyDown_ = false;
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            isDKeyDown_ = true;
        }
        if (Input.GetKeyUp(KeyCode.D))
        {
            isDKeyDown_ = false;
        }
        if ((isDKeyDown_ && isAKeyDown_))
        {


        }
        else {
            if (isDKeyDown_)
            {
               // Debug.Log("cmd right..");
                this.getCmdReader((ICmdReader cmdr) => {
                    cmdr.SendCmd(new CmdMove(CmdMove.ORI_RIGHT));
                });
            }
            if (isAKeyDown_)
            {
                //Debug.Log("cmd left..");
                this.getCmdReader((ICmdReader cmdr) => {
                    cmdr.SendCmd(new CmdMove(CmdMove.ORI_LEFT));
                });
            }
        }

        //var axis = Input.GetAxis("Horizontal");
        //if (Mathf.Abs(axis)>MIN_ABS) {
        //    if (axis > 0)
        //    {
        //        Debug.Log("cmd right..");
        //        this.getCmdReader((ICmdReader cmdr)=> {
        //            cmdr.SendCmd(new CmdMove(1));
        //        });
        //    }
        //    else {
        //        Debug.Log("cmd left..");
        //        this.getCmdReader((ICmdReader cmdr) => {
        //            cmdr.SendCmd(new CmdMove(-1));
        //        });
        //    }
        //}
        var axis = Input.GetAxis("Fire1");
        if (Mathf.Abs(axis) > MIN_ABS) {
            //Debug.Log("cmd fire");
            this.getCmdReader((ICmdReader cmdr) => {
                cmdr.SendCmd(new CmdFire());
            });
        }

        if (Input.GetKeyDown(KeyCode.K)) {
            this.getCmdReader((ICmdReader cmdr)=> {
                cmdr.SendCmd(new CmdJump());
            });
        }
    }
}
